package cn.qhuan.modules.service.impl;

import cn.qhuan.modules.dtos.AcademicAuthDto;
import cn.qhuan.modules.dtos.AcademicDto;
import cn.qhuan.modules.entity.Academic;
import cn.qhuan.modules.mapper.AcademicMapper;
import cn.qhuan.modules.service.IAcademicService;
import cn.qhuan.modules.constos.PageResponseResult;
import cn.qhuan.modules.constos.StatusConstos;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 学术成果表
 * @Author: 司翔宇
 * @Date:   2020-12-30
 * @Version: V1.0
 */
@Service
public class AcademicServiceImpl extends ServiceImpl<AcademicMapper, Academic> implements IAcademicService {
    @Autowired
    private AcademicMapper academicMapper;



    @Override
    public Result<?> loadAcademicByPushTime(AcademicDto dto) {
        //1.检查参数
        if (dto == null || dto.getArtistId() == null){
            return Result.error("缺少参数");
        }
        //根据时间降序进行排序
        //
        dto.checkParam();
        IPage pageParam =new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Academic> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getArtistId())){
            lambdaQueryWrapper.eq(Academic::getArtistId,dto.getArtistId())
                    .eq(Academic::getState,StatusConstos.STATUS_PASS)
                    .orderByDesc(Academic::getPushTime);
        }

        //3.封装结果返回
        IPage pageList = page(pageParam, lambdaQueryWrapper);
        PageResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());
        return responseResult;
    }

    @Override
    public Result<?> updateStatus(AcademicAuthDto dto, Integer type) {
        //1.检查参数
        if (dto ==null || dto.getId() == null ){
            return Result.error("缺少参数");
        }
        //2.查询作品信息
        Academic academic = academicMapper.selectById(dto.getId());

        if (academic == null){
            return Result.error("学术成果不存在");
        }
        //3.审核失败
        if (type.equals(StatusConstos.STATUS_FAIL)){
            updateArtist(academic,StatusConstos.STATUS_FAIL);
        }else if (type.equals(StatusConstos.STATUS_PASS)){
            updateArtist(academic,StatusConstos.STATUS_PASS);
        }
        return Result.OK("修改成功");
    }
    /**
     * 修改作品信息
     * @param academic
     * @param status
     *
     */
    private void updateArtist (Academic academic,Integer status){
        academic.setState(status);
        academicMapper.updateById(academic);
    }
}

